import React from 'react'
import { getStorage } from '@designable/event-system'
/**
 * accessKey 权限标识符
 * noAccessRender  没有权限要显示的dom
 * isMain  是否是主应用  默认不是 拿的数据类型不一样
 */
interface AccessBoxProps {
  children: React.ReactNode
  accessKey: string
  noAccessRender?: React.ReactNode
}

const adminQiankunGlobalState = getStorage('adminQiankunGlobalState') || {}

export default function AccessBox(props: AccessBoxProps) {
  const { accessKey, children, noAccessRender } = props
  const accessArr: string[] = adminQiankunGlobalState.accessArr || []
  if (accessArr.length === 0) return <>{children}</>
  const hasAccess = accessKey ? accessArr.includes(accessKey) : true // 是否有权限
  // 如果没有权限
  if (!hasAccess) {
    return <>{noAccessRender || null}</>
  }
  // 有权限
  return <>{children}</>
}
